package controllers

import (
    "fmt"

    "github.com/astaxie/beego"

    "yikexing.com.cn/wyws/models"
    "yikexing.com.cn/wyws/bus"
)

// 车辆controller
type VehicleController struct {
    BaseController
}

// @Title 新建一个车辆
// @Description 新建一个车辆
// @Param   body    body    models.VehicleCommand    true    "车辆实体"
// @Success 200  {object} models.VehicleReturnInfo
// @router / [post]
func (this *VehicleController) Post() {

    var cmd = models.VehicleCommand{}
    err := this.parseAndValidateCommand(cmd)
    beego.Debug(fmt.Sprintf(" in post after validate , the cmd is %s", cmd))
    // 新建
    if err != nil {
        //命令解析出错
        beego.Debug(err)
        this.returnFormatJsonData(1001, nil, err.Error())
    } else {
        dataStack := make(map[string]interface{})
        err = bus.DispatchWithDataStack(&cmd, dataStack)
        if err != nil {
            this.returnFormatJsonData(1002, nil, err.Error())
        } else {
            this.returnFormatJsonData(0, dataStack["VehicleCommand"], "获取成功")
        }
    }

    this.ServeJSON()
}

// @Title 更新车辆
// @Description 更新车辆
// @Param    id      path    string                       true    "车辆的唯一标示"
// @Param    body    body    models.VehicleUpdateCommand  true    "车辆的内容实体"
// @Success 200 {object} models.VehicleReturnInfo
// @router /:id [put]
func (this *VehicleController) Put() {

    id, err := this.GetInt(":id")
    if err != nil {
        this.returnFormatJsonData(1, nil, "检查id是否正确")
        this.ServeJSON()
        return
    }

    var cmd = models.VehicleUpdateCommand{}
    cmd.Id = int64(id)  
    err = this.parseAndValidateCommand(cmd)
    beego.Debug(fmt.Sprintf(" in post after validate , the cmd is %s", cmd))

    if err != nil {
        //命令解析出错
        beego.Debug(err)
        this.returnFormatJsonData(1001, nil, err.Error())
    } else {
        dataStack := make(map[string]interface{})
        err = bus.DispatchWithDataStack(&cmd, dataStack)
        if err != nil {
            this.returnFormatJsonData(1002, nil, err.Error())
        } else {
            this.returnFormatJsonData(0, dataStack["VehicleUpdateCommand"], "获取成功")
        }
    }
    this.ServeJSON()
}

// @Title 车辆检索
// @Description 根据检索条件查询车辆信息
// @Param   body body string true "通用查询条件"
// @Success 200 {object} models.VehicleReturnInfo
// @Failure 403 结果为空
// @router /search [post]
func (this *VehicleController) GetAll() {

    var cmd = models.SearchVehicleCommand{}
    cmd.WhereSql = this.Ctx.Input.RequestBody
    isValid := this.checkCommand(cmd)
    if isValid == nil {
        dataStack := make(map[string]interface{})
        err := bus.DispatchWithDataStack(&cmd, dataStack)
        if err == nil {
            this.returnFormatJsonData(0, dataStack["SearchVehicleCommand"], "检索车辆信息成功!")
        } else {
            this.returnFormatJsonData(1, nil, fmt.Sprintf("检索备车辆信息失败!错误信息:%s", err))
        }
    } else {
        this.returnFormatJsonData(1001, nil, isValid.Error())
    }
    this.ServeJSON()
}

// @Title 删除车辆
// @Description 删除车辆
// @Param   id    path    string    true    "车辆id"
// @Success 200 {object} models.ReturnInfo
// @router /:id [delete]
func (this *VehicleController) Delete() {

    id, err := this.GetInt(":id")
    if err != nil {
        this.returnFormatJsonData(1, nil, "检查id是否正确")
        this.ServeJSON()
        return
    }
    var cmd = models.DeleteVehicleCommand{}
    cmd.Id = int64(id)
    isValid := this.checkCommand(cmd)
    if isValid == nil {
        err := bus.Dispatch(&cmd)
        if err == nil {
            this.returnFormatJsonData(0, nil, "删除车辆信息成功!")
        } else {
            this.returnFormatJsonData(1, nil, fmt.Sprintf("删除车辆信息失败!错误信息:%s", err))
        }
    } else {
        this.returnFormatJsonData(1001, nil, isValid.Error())
    }
    this.ServeJSON()
}


// @Title 车辆所属车队信息（树形结构）
// @Description 根据车辆id获取车队的信息
// @Param   id    path    string    true    "车辆id"
// @Success 200 {object} models.FleetOfVehicleReturnInfo
// @Failure 403 结果为空
// @router /:id/ [get]
func (this *VehicleController) GetFeets() {

    id, err := this.GetInt(":id")
    if err != nil {
        this.returnFormatJsonData(1, nil, "检查id是否正确")
        this.ServeJSON()
        return
    }

    var cmd = models.FleetOfVehicleCommand{}
    cmd.Id = int64(id)
    dataStack := make(map[string]interface{})
    err = bus.DispatchWithDataStack(&cmd, dataStack)
    if err == nil {
        this.returnFormatJsonData(0, dataStack["FleetOfVehicleCommand"], "获取车辆的车队信息成功!")
    } else {
        this.returnFormatJsonData(1, nil, fmt.Sprintf("检索备车辆的车队信息失败!错误信息:%s", err))
    }
    this.ServeJSON()
}

